package janelas;

import outros.CampoMask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;
import outros.Documento;
import outros.ModeloTabela;
import outros.Util;
import persistencia.DAOFornecedor;
import transporte.TOFornecedor;

public class Fornecedor extends javax.swing.JInternalFrame implements JanelaPai {
    private TOFornecedor to = new TOFornecedor();
    private DAOFornecedor dao = new DAOFornecedor();
    private ArrayList<TOFornecedor> lista = new ArrayList();
    private ArrayList<TOFornecedor> listaSel = new ArrayList();
    private ModeloTabela modelo;
    private Util util = new Util();
    private boolean alterar = false;
    private String ultimaBusca = "";
    private JScrollBar barra;

    public Fornecedor() throws ParseException {
        initComponents();
        iniciarTabela();
        botExcluir.setVisible(false);
        camInfo.setText("~ Novo registro ~");
        botExcluir.setEnabled(false);
        camBusca.requestFocus();
        barra = tabelaScroll.getVerticalScrollBar();        
    }

    public void reiniciar() {
        camInfo.setText("~ Novo registro ~");
        camBusca.setText("");
        ultimaBusca = "";
        alterar = false;
        modelo.apagarLinhas();
        botExcluir.setEnabled(false);
        camBusca.requestFocus();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-getWidth())/2, (int)((screenSize.height-getHeight())/3), getWidth(), getHeight());
        limparCampos();
        lista = dao.selecionar("");
        buscar("");
    }

    private void iniciarTabela() {
        ArrayList dados = new ArrayList();
        String[] colunas = new String[]{"ID","Nome"};
        modelo = new ModeloTabela(dados, colunas);
        tabela.setModel(modelo);
        tabela.setCellSelectionEnabled(false);
        tabela.setRowSelectionAllowed(true);
        tabela.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tabela.setRowHeight(25);
        for (int i=0;i<2;i+=1) {
            tabela.getColumnModel().getColumn(i).setResizable(false);
        }
        tabela.getColumnModel().getColumn(1).setPreferredWidth(800);
        tabela.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                preencherCampos();
            }
        });
        tabela.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode()==KeyEvent.VK_UP || e.getKeyCode()==KeyEvent.VK_DOWN) {
                    preencherCampos();
                }
            }
        });
        modelo.setTabela(tabela, 1);
        final JTableHeader header = tabela.getTableHeader();
        header.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                preencherCampos();
            }
        });        
    }

    private void preencherCampos() {
        camInfo.setText("~ Alterar registro ~");
        botExcluir.setEnabled(true);
        to = ache(Integer.parseInt((String)tabela.getValueAt(tabela.getSelectedRow(), 0)));
        if (!to.equals(null)) {
            camNumero.setText(to.getNumero().toString());
            camNome.setText(to.getNome());
            camEndereco.setText(to.getEndereco());
            camCidade.setText(to.getCidade());
            camSetor.setText(to.getSetor());
            camEstado.setSelectedIndex(to.getSigla(to.getEstado()));
            camCep.setText(to.getCep());
            camTelefone.setText(to.getTelefone());
            camContato.setText(to.getContato());
            alterar = true;
        }        
    }

    private TOFornecedor ache(int numero) {
        for (int i=0;i<listaSel.size();i+=1) {
            if (listaSel.get(i).getNumero()==numero) {
                return listaSel.get(i);
            }
        }
        return null;
    }

    private void limparCampos() {
        if (!alterar) {
            camNumero.setText("");
        }
        camNome.setText("");
        camEndereco.setText("");
        camCidade.setText("");
        camSetor.setText("");
        camEstado.setSelectedIndex(0);
        camCep.setText("");
        camTelefone.setText("");
        camContato.setText("");
        camNome.requestFocus();
    }

    private boolean montaFornecedor() {
        if (alterar) {
            to.setNumero(Integer.parseInt(camNumero.getText()));
        }
        if (!camNome.getText().isEmpty()) {
            to.setNome(camNome.getText());
        } else {
            camNome.requestFocus();
            JOptionPane.showMessageDialog(null, "Preencha o campo nome.");
            return false;
        }
        if (!camEndereco.getText().isEmpty()) {
            to.setEndereco(camEndereco.getText());
        } else {
            camEndereco.requestFocus();
            JOptionPane.showMessageDialog(null, "Preencha o campo endereco.");
            return false;
        }
        if (!camSetor.getText().isEmpty()) {
            to.setSetor(camSetor.getText());
        } else {
            camSetor.requestFocus();
            JOptionPane.showMessageDialog(null, "Preencha o campo setor.");
            return false;
        }
        if (!camCidade.getText().isEmpty()) {
            to.setCidade(camCidade.getText());
        } else {
            camCidade.requestFocus();
            JOptionPane.showMessageDialog(null, "Preencha o campo cidade.");
            return false;
        }
        to.setEstado((String)camEstado.getSelectedItem());
        if (!camCep.getText().isEmpty()) {
            to.setCep(camCep.getText().replaceAll("-", ""));
        } else {
            camCep.requestFocus();
            JOptionPane.showMessageDialog(null, "Preencha o campo cep.");
            return false;
        }
        if (!camTelefone.getText().isEmpty()) {
            to.setTelefone(camTelefone.getText().replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("-", ""));
        } else {
            camTelefone.requestFocus();
            JOptionPane.showMessageDialog(null, "Preencha o campo telefone.");
            return false;
        }
        /*if (!camContato.getText().isEmpty()) {
            to.setContato(camContato.getText());
        } else {
            camContato.requestFocus();
            JOptionPane.showMessageDialog(null, "Preencha o campo e-mail.");
            return false;
        }*/
        return true;
    }

    private void excluir() {
        if (JOptionPane.showConfirmDialog(null, "Tem certeza que deseja excluír?")==0) {
            if (dao.excluir(Integer.parseInt(camNumero.getText())).equals("")) {
                buscar(ultimaBusca);
                barra.setValue(0);
            }
        }
    }

    private void salvar() {
        if (montaFornecedor()) {
            if (alterar) {
                if (dao.alterar(to).equals("")) {
                    JOptionPane.showMessageDialog(null, "Registro alterado com sucesso.");
                    buscar(ultimaBusca);
                    barra.setValue(0);
                }
            } else {
                if (dao.criar(to).equals("")) {
                    JOptionPane.showMessageDialog(null, "Cadastro efetuado com sucesso.");
                    buscar(ultimaBusca);
                    barra.setValue(0);
                }
            }
        }
    }

    private void buscar(String busca) {
        ultimaBusca = busca;
        if (busca.length()>0) {
            listaSel.clear();
            modelo.apagarLinhas();
            String b = util.retirarAcentos(busca.toLowerCase());
            String atual = "";
            for (int i=0;i<lista.size();i+=1) {
                atual = util.retirarAcentos(lista.get(i).getNome().toLowerCase());
                if (atual.startsWith(b)) {
                    listaSel.add(lista.get(i));
                }
            }
            for (int i=0;i<listaSel.size();i+=1) {
                modelo.addRow(new String[]{
                    Integer.toString(listaSel.get(i).getNumero()),
                    listaSel.get(i).getNome()});
            }
        } else {
            listaSel.clear();
            modelo.apagarLinhas();
            for (int i=0;i<lista.size();i+=1) {
                listaSel.add(lista.get(i));
                modelo.addRow(new String[]{
                    Integer.toString(listaSel.get(i).getNumero()),
                    listaSel.get(i).getNome()});
            }
        }
        if (modelo.getRowCount()>0) {
            tabela.setRowSelectionInterval(0, 0);
            preencherCampos();
        }
        //preencherCampos();
        /*if (busca.length()==0 || busca.length()>2) {
            ArrayList<TOFornecedor> al = dao.selecionar(util.retirarAcentos(busca));
            if (al==null || al.size()==0) {
                modelo.apagarLinhas();
                JOptionPane.showMessageDialog(null, "Nenhum resultado obtido.");
            } else {
                ultimaBusca = camBusca.getText();
                modelo.apagarLinhas();
                for (int i=0;i<al.size();i+=1) {
                    modelo.addRow(new String[]{
                        Integer.toString(al.get(i).getNumero()),
                        al.get(i).getNome()});
                }
                tabela.setRowSelectionInterval(0, 0);
                tabela.requestFocus();
                preencherCampos();
            }
        } else {
            modelo.apagarLinhas();
            JOptionPane.showMessageDialog(null, "Preencha o campo de busca com pelo menos 3 caracteres ou nenhum para buscar todos os registros.");
        }*/
    }

    private void novo() {
        camInfo.setText("~ Novo registro ~");
        modelo.apagarLinhas();
        alterar = false;
        limparCampos();
        botExcluir.setEnabled(false);
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painelConsulta = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        camBusca = new javax.swing.JTextField();
        tabelaScroll = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        camNumero = new javax.swing.JTextField();
        camNumero.setDocument(new Documento(true, 3));
        jLabel2 = new javax.swing.JLabel();
        camNome = new javax.swing.JTextField();
        camInfo = new javax.swing.JLabel();
        camEndereco = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        camCidade = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        camSetor = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        camEstado = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        camCep = new CampoMask("#####-###");
        jLabel8 = new javax.swing.JLabel();
        camTelefone = new CampoMask("(##)####-####");
        camContato = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        botExcluir = new javax.swing.JButton();
        botSalvar = new javax.swing.JButton();
        botLimpar = new javax.swing.JButton();
        botNovo = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Fornecedores");
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        camBusca.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        camBusca.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                camBuscaKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(camBusca, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(camBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabela.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Número", "Nome"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setColumnSelectionAllowed(true);
        tabela.getTableHeader().setReorderingAllowed(false);
        tabelaScroll.setViewportView(tabela);
        tabela.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabela.getColumnModel().getColumn(0).setResizable(false);
        tabela.getColumnModel().getColumn(1).setResizable(false);
        tabela.getColumnModel().getColumn(1).setPreferredWidth(250);

        javax.swing.GroupLayout painelConsultaLayout = new javax.swing.GroupLayout(painelConsulta);
        painelConsulta.setLayout(painelConsultaLayout);
        painelConsultaLayout.setHorizontalGroup(
            painelConsultaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelConsultaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelConsultaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(tabelaScroll, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE))
                .addContainerGap())
        );
        painelConsultaLayout.setVerticalGroup(
            painelConsultaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelConsultaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabelaScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(painelConsulta);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel1.setText("Número");

        camNumero.setEditable(false);
        camNumero.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel2.setText("Nome");

        camNome.setDocument(new Documento(false, 100));
        camNome.setFont(new java.awt.Font("Segoe UI", 0, 13));

        camInfo.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        camInfo.setText("...");

        camEndereco.setDocument(new Documento(false, 150));
        camEndereco.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel3.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel3.setText("Endereço");

        jLabel4.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel4.setText("Cidade");

        camCidade.setDocument(new Documento(false, 100));
        camCidade.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel5.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel5.setText("Setor");

        camSetor.setDocument(new Documento(false, 100));
        camSetor.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel6.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel6.setText("Estado");

        camEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-", "AC", "AL", "AM", "AP", "BA", "CE", "DF", "ES", "GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", "PR", "RJ", "RN", "RO", "RR", "RS", "SC", "SE", "SP", "TO" }));

        jLabel7.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel7.setText("CEP");

        camCep.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camCep.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel8.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel8.setText("Telefone");

        camTelefone.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camTelefone.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        camContato.setDocument(new Documento(false, 100));
        camContato.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel9.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel9.setText("E-mail");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(camEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(camCep, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(camTelefone, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(camContato, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(camNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(camNome, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
                                .addComponent(jLabel2)))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(camCidade, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(camSetor, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(camEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(camInfo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(camInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(camNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(camNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(camEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(camEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(camSetor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(camCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel8)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(camTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camContato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        botExcluir.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        botExcluir.setText("Excluír");
        botExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botExcluirActionPerformed(evt);
            }
        });

        botSalvar.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botSalvar.setText("Salvar");
        botSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botSalvarActionPerformed(evt);
            }
        });

        botLimpar.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botLimpar.setText("Limpar");
        botLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botLimparActionPerformed(evt);
            }
        });

        botNovo.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botNovo.setText("Novo");
        botNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botNovoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botLimpar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botSalvar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botExcluir)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botExcluir)
                    .addComponent(botSalvar)
                    .addComponent(botLimpar)
                    .addComponent(botNovo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(148, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel4);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1015)/2, (screenSize.height-500)/2, 1015, 500);
    }// </editor-fold>//GEN-END:initComponents

    private void botExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botExcluirActionPerformed
        excluir();
}//GEN-LAST:event_botExcluirActionPerformed

    private void botSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botSalvarActionPerformed
        salvar();
}//GEN-LAST:event_botSalvarActionPerformed

    private void botLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botLimparActionPerformed
        limparCampos();
}//GEN-LAST:event_botLimparActionPerformed

    private void botNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botNovoActionPerformed
        novo();
}//GEN-LAST:event_botNovoActionPerformed

    private void camBuscaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_camBuscaKeyReleased
        buscar(camBusca.getText());
    }//GEN-LAST:event_camBuscaKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botExcluir;
    private javax.swing.JButton botLimpar;
    private javax.swing.JButton botNovo;
    private javax.swing.JButton botSalvar;
    private javax.swing.JTextField camBusca;
    private javax.swing.JTextField camCep;
    private javax.swing.JTextField camCidade;
    private javax.swing.JTextField camContato;
    private javax.swing.JTextField camEndereco;
    private javax.swing.JComboBox camEstado;
    private javax.swing.JLabel camInfo;
    private javax.swing.JTextField camNome;
    private javax.swing.JTextField camNumero;
    private javax.swing.JTextField camSetor;
    private javax.swing.JTextField camTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel painelConsulta;
    private javax.swing.JTable tabela;
    private javax.swing.JScrollPane tabelaScroll;
    // End of variables declaration//GEN-END:variables

}
